package p2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-06-07
 * Time: 15:13
 */
public class d1 {
    private static boolean isQuit = false;

    //线程核心操作:1.start
    //start和run的区别:
    //run:描述了线程要执行的任务,线程的入口
    //start:调用系统函数(根据不同的系统,调用不同的API),在系统内核中创建线程
    public static void main1(String[] args) {
        //任意创建线程,任何的线程都可以创建其他线程
        Thread t1 = new Thread(() -> {
            Thread t2 = new Thread(() -> {
            });
            t2.start();
        });
        t1.start();
    }

    public static void main2(String[] args) {
        Thread t = new Thread(() -> {
        });
        t.start();
//        t.start();//已经start之后不能再次start,java中希望一个Thread对象,只能对应到一个线程,只能调用一次start
    }

    //2.线程终止:让一个线程结束~~,A线程想让B线程结束,就是想办法让B的run方法执行完毕
    public static void main3(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (!isQuit) {
                System.out.println("Hello Thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t 线程执行结束");
        });

        t.start();
        Thread.sleep(2000);
        System.out.println("main 线程尝试终止 t 线程");
        isQuit = true;
        System.out.println("t 线程终止");
    }

    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("1");
            }
        });
        t.start();
        System.out.println("2");
    }
}
